package com.bishe.o2oo.web.frontend;

import com.alibaba.fastjson.JSONObject;
import com.bishe.o2oo.dto.UserProductMapExecution;
import com.bishe.o2oo.entity.PersonInfo;
import com.bishe.o2oo.entity.Product;
import com.bishe.o2oo.entity.Shop;
import com.bishe.o2oo.entity.UserProductMap;
import com.bishe.o2oo.service.ProductService;
import com.bishe.o2oo.service.UserProductMapService;
import com.bishe.o2oo.util.HttpServletRequestUtil;
import com.bishe.o2oo.util.NOUtil;
import com.bishe.o2oo.util.pay.AliPayUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("/frontend")
public class ProductPayController {
	private final static Logger LOG = LoggerFactory.getLogger(ProductPayController.class);
	@Autowired
	private UserProductMapService userProductMapService;

	@Autowired
	private ProductService productService;

	/**
	 * 列出某个顾客的商品消费信息
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/productpay")
	@ResponseBody
	private Map<String, Object> listUserProductMapsByCustomer(HttpServletRequest request) {
		Map<String, Object> modelMap = new HashMap<String, Object>();
		// 获取前台传递过来的productId
		long productId = HttpServletRequestUtil.getLong(request, "productId");
		PersonInfo user = (PersonInfo) request.getSession().getAttribute(
				"user");

		if (productId != -1 && user != null) {
			Long orderNo = NOUtil.getOrderNo();

			Product product = productService.getProductById(productId);
			UserProductMap userProductMap = new UserProductMap();
			userProductMap.setUser(user);
			userProductMap.setOperator(user);
			userProductMap.setProduct(product);
			userProductMap.setPoint(product.getPoint());
			userProductMap.setShop(product.getShop());
			userProductMap.setOrderStatus(1);
			userProductMap.setOrderNo(orderNo);
			userProductMapService.addUserProductMap(userProductMap);


			BigDecimal promotionPrice = new BigDecimal(product.getPromotionPrice());
			String payFormText = AliPayUtil.getAliPayFormText(promotionPrice, product.getProductName(),
			product.getProductDesc(), String.valueOf(orderNo));
			modelMap.put("success", true);
			modelMap.put("payFormText", payFormText);

		} else {
			modelMap.put("success", false);
			modelMap.put("errMsg", "empty productId");
		}


		return modelMap;
	}

	/**
	 * 回调地址
	 * @param request
	 * @return
	 */
	@RequestMapping("/productNotifyUrl")
	@ResponseBody
	public String notifyUrl(HttpServletRequest request) {
		Map<String, String> params = AliPayUtil.getParams(request);

		String out_trade_no = request.getParameter("out_trade_no");
		String trade_no = request.getParameter("trade_no");
		String trade_status = request.getParameter("trade_status");
		String passbackParams = request.getParameter("passback_params");
		String buyer_id = request.getParameter("buyer_id");
		String buyer_logon_id = request.getParameter("buyer_logon_id");
		boolean verify_result  = AliPayUtil.verifySign(params);

		if (verify_result) {
			// 验证通过
			LOG.info("支付成功---------------->");
			UserProductMap userProductMap = new UserProductMap();
			userProductMap.setOrderNo(Long.valueOf(out_trade_no));
			userProductMap.setOrderStatus(2);
			userProductMapService.updateUserProductMapOrderStatus(userProductMap);
			return "yes";

		} else {
			LOG.info("支付失败---------------->");

			return "no";
		}

	}
}
